package com.jinxiangyang.controller;


import com.jinxiangyang.annotation.LoginLog;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Menu;
import com.jinxiangyang.domain.UserInfo;
import com.jinxiangyang.domain.vo.LoginBody;
import com.jinxiangyang.service.ISysMenuService;
import com.jinxiangyang.service.LoginService;
import com.jinxiangyang.utils.SecurityUtils;
import com.jinxiangyang.utils.constants.Constants;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jinxiangyang
 * @date 2023/8/14 13:57
 * @description TODO
 **/


@Api(tags = "用户登录接口")
@RequestMapping("/user")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private ISysMenuService menuService;

    /**
     * 登录方法
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @LoginLog
    @OperateLog(title = "用户登录接口", businessType = BusinessType.OTHER, operatorType = OperatorType.OTHER)
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        System.out.println(token);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        UserInfo user = SecurityUtils.getLoginUser().getUser();
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<Menu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

}
